package com.caocaog.market.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.caocaog.market.entity.ImagesBanner;
import com.caocaog.market.entity.ImagesCarousel;
import com.caocaog.market.mapper.ImagesCarouselMapper;
import com.caocaog.market.service.ImagesCarouselService;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.vo.LoginUser;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 轮播图管理
 *
 * @author chechen
 * @date 2021-12-28
 */
@Service
@Transactional
public class ImagesCarouselServiceImpl extends ServiceImpl<ImagesCarouselMapper, ImagesCarousel> implements ImagesCarouselService {

    @Override
    public IPage<ImagesCarousel> queryImagesCarouselPage(Page<ImagesCarousel> page, ImagesCarousel imagesCarousel) {
        return super.baseMapper.queryImagesCarouselPage(page, imagesCarousel);
    }

    @Override
    public ImagesCarousel getImagesCarouselByTypeSource(int positionType) {
        return super.baseMapper.getImagesCarouselByType(positionType);
    }

    /**
     * 获取全部轮播图
     * @return 全部的轮播图
     */
    @Override
    public List<ImagesBanner> getImagesCarouselList() {
        return super.baseMapper.getImagesCarouse();
    }

    @Override
    public String addImagesCarousel(ImagesCarousel imagesCarousel) {
        imagesCarousel.clearFields();
        imagesCarousel.setCreateBy(((LoginUser) SecurityUtils.getSubject().getPrincipal()).getUsername());

        super.baseMapper.insert(imagesCarousel);
        if (imagesCarousel.getId() == null)
            throw new JeecgBootException("新增轮播图信息失败");
        return imagesCarousel.getId();
    }

    @Override
    public int updateImagesCarousel(ImagesCarousel imagesCarousel) {
        imagesCarousel.clearFields();
        imagesCarousel.setUpdateBy(((LoginUser) SecurityUtils.getSubject().getPrincipal()).getUsername());

        int updateRecordCount = super.baseMapper.updateById(imagesCarousel);
        if (updateRecordCount == 0)
            throw new JeecgBootException("更新轮播图信息失败, id-> " + imagesCarousel.getId());
        return updateRecordCount;
    }

    @Override
    public int deleteImagesCarousel(Long id) {
        int deleteRecordCount = super.baseMapper.deleteById(id);

        if (deleteRecordCount == 0)
            throw new JeecgBootException("删除轮播图信息失败, id-> " + id);
        return deleteRecordCount;
    }

}
